package com.book.controller;


import com.book.config.JwtUtil;
import com.book.pojo.MonthlyPass;
import com.book.pojo.Readers;
import com.book.pojo.Signin;
import com.book.service.MonthlyPassService;
import com.book.service.ReadersService;
import com.book.service.SigninService;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fk
 * @since 2023-10-12 17:37:48
 */
@RestController
@RequestMapping("/signin")
public class SigninController {

    @Autowired
    private SigninService signinService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private MonthlyPassService monthlyPassService;
    @Autowired
    private ReadersService readersService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("getById")
    public List<Signin> getById(@RequestHeader String unauthorized){
        //从token中解析用户id
        Claims claims = jwtUtil.parseJwt(unauthorized);
        Long userId = Long.valueOf(claims.getId());
        return signinService.getById(userId);
    }

    @PostMapping("addById")
    public int addById(@RequestHeader String unauthorized){
        Claims claims = jwtUtil.parseJwt(unauthorized);
        Long readerId = Long.valueOf(claims.getId());
        addByPass(readerId);
        getByReaderId(readerId);

        return signinService.addById(readerId);
    }

    public void addByPass(Long readerId){
        MonthlyPass monthlyPass = new MonthlyPass();
        monthlyPass.setReaderId(readerId);
        monthlyPass.setMonthlyCome(1);
        monthlyPass.setMonthlyNum("+1");
        monthlyPassService.addByPass(monthlyPass);
    }

    public void getByReaderId(Long readerId){
        Readers readers = readersService.getByReaderId(readerId);
        readersService.getupdate(readers);
    }

    @GetMapping("ReaderMes")
    public int ReaderMes(String sendsMsg) {
        String sendMsg = redisTemplate.boundValueOps("sendMsg").get().toString();
        if (!Objects.equals(sendsMsg, sendMsg)) {
            return 2;
        }
        return 1;
    }
    @PutMapping("updatePassword")
    public int updatePassword(@RequestHeader String unauthorized,String password1,String password2){
        if(!Objects.equals(password1,password2)){
            return 2;
        }
        Claims claims = jwtUtil.parseJwt(unauthorized);
        Long userId = Long.valueOf(claims.getId());
        Readers byid = readersService.getByid(userId);
        String s = ShiroUtils.generateSalt(6);
        String md5 = ShiroUtils.encryptPassword("md5", password2, s, 7);
        byid.setPassword(md5);
        byid.setSalt(s);
        return readersService.updatepassword(byid);
    }

    @GetMapping("getAll")
    public Readers getAll(@RequestHeader String unauthorized){
        //从token中解析用户id
        Claims claims = jwtUtil.parseJwt(unauthorized);
        Long userId = Long.valueOf(claims.getId());
        return readersService.getAll(userId);
    }

    @PutMapping("updateUser")
    public void updateUser(Readers readers){
        readersService.updateUser(readers);
    }

    @DeleteMapping("deleteReaders")
    public void deleteReaders(@RequestHeader String unauthorized){
        //从token中解析用户id
        Claims claims = jwtUtil.parseJwt(unauthorized);
        Long userId = Long.valueOf(claims.getId());
        readersService.deleteReaders(userId);
    }

}
